package com.ksy.oss;

import android.content.Context;

import com.ksy.common.utils.ParamsUtil;
import com.qiniu.android.storage.UploadManager;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * Created by ksy on 2018/3/29.
 */

public class OssClient {

    private UploadManager uploadManager;

    public enum FileType {
        Image("src/photos"),
        Audio("src/audios"),
        Video("src/videos");

        public final String path;

        FileType(String path) {
            this.path = path;
        }
    }

    public enum Status {
        Error_Oss("上传至云服务器失败"),
        Error_File("上传文件失败"),
        Skip("跳过"),
        Success("上传成功"),;

        public final String msg;

        Status(String msg) {
            this.msg = msg;
        }
    }

    private OssClient() {
    }

    private static OssClient ossClient;

    public static OssClient getInstance() {
        if (ossClient == null) {
            synchronized (OssClient.class) {
                if (ossClient == null) {
                    ossClient = new OssClient();
                    return ossClient;
                }
            }
        }
        return ossClient;
    }

    public UploadManager getUpper() {
        if (uploadManager == null) {
            uploadManager = new UploadManager();
        }
        return uploadManager;
    }

    public interface UpdatesListener {
        void onFinish(boolean isSuccess, List<OssResult> results, List<String> ossUrls);
    }

    public interface UpdateListener {
        void onFinish(boolean isSuccess, String ossPath);
    }

    public void update(Context context, final List<ParamsUtil.ParamsTwo<FileType, String>> fileList, final UpdatesListener listener) {
        final List<OssResult> ossResults = new ArrayList<>();
        for (int i = 0; i < fileList.size(); i++) {
            ParamsUtil.ParamsTwo<FileType, String> paramsTwo = fileList.get(i);
            UpdateFileRunnable runnable = new UpdateFileRunnable(context, paramsTwo.first, paramsTwo.second, new UpdateFileRunnable.UpdateListener() {
                @Override
                public void onProgress(UpdateFileRunnable runnable, String bucketName, String objectKey, String uploadFilePath, long currentSize, long totalSize) {

                }

                @Override
                public void onFinish(UpdateFileRunnable runnable, Status status, FileType fileType, String aliPath, String uploadFilePath) {
                    int tag = runnable.getTag();
                    OssResult ossResult = new OssResult();
                    ossResult.index = tag;
                    ossResult.srcUrl = uploadFilePath;
                    ossResult.aliUrl = aliPath;
                    ossResult.status = status;
                    ossResult.fileType = fileType;
                    ossResults.add(ossResult);
                    if (ossResults.size() == fileList.size()) {
                        if (listener != null) {
                            Collections.sort(ossResults, new Comparator<OssResult>() {
                                @Override
                                public int compare(OssResult o1, OssResult o2) {
                                    return o1.index - o2.index;
                                }
                            });
                            List<String> resultUrls = new ArrayList<>();
                            boolean isSuccess = true;
                            for (OssResult result : ossResults) {
                                boolean success = (result.status == OssClient.Status.Success || result.status == OssClient.Status.Skip);
                                if (result.status == OssClient.Status.Success) {
                                    resultUrls.add(result.aliUrl);
                                }
                                result.isSuccess = result.status == OssClient.Status.Success;
                                isSuccess = isSuccess && success;
                            }
                            //返回 是否成功  成功的url
                            listener.onFinish(isSuccess, ossResults, resultUrls);
                        }
                    }
                }
            });
            runnable.setTag(i);
            runnable.run();
        }
    }

    public void update(Context context, final ParamsUtil.ParamsTwo<FileType, String> file, final UpdateListener listener) {
        UpdateFileRunnable runnable = new UpdateFileRunnable(context, file.first, file.second, new UpdateFileRunnable.UpdateListener() {
            @Override
            public void onProgress(UpdateFileRunnable runnable, String bucketName, String objectKey, String uploadFilePath, long currentSize, long totalSize) {
            }

            @Override
            public void onFinish(UpdateFileRunnable runnable, Status status, FileType fileType, String aliPath, String uploadFilePath) {
                int tag = runnable.getTag();
                OssResult ossResult = new OssResult();
                ossResult.srcUrl = uploadFilePath;
                ossResult.aliUrl = aliPath;
                ossResult.status = status;
                ossResult.fileType = fileType;
                if (listener != null) {
                    boolean isSuccess = (ossResult.status == OssClient.Status.Success || ossResult.status == OssClient.Status.Skip);
                    //返回 是否成功  成功的url
                    ossResult.isSuccess = isSuccess;
                    listener.onFinish(isSuccess, aliPath);
                }
            }
        });
        runnable.run();
    }
}
